package cn.xd.core.utils;

import cn.hutool.extra.spring.SpringUtil;
import cn.xd.core.service.BasicMpService;

import java.lang.reflect.ParameterizedType;
import java.util.Map;

/**
 * @author : mirzaLin
 * @data : 2023/11/19
 * @description : 核心工具类
 **/
public class CoreUtils {

    public static BasicMpService getBasicMpService(Class<?> clazz) {
        BasicMpService<?> basicMpService = null;
        Map<String, ?> beansOfType = SpringUtil.getBeansOfType(BasicMpService.class);
        for (Object bean : beansOfType.values()) {
            Class<?> beanClass = bean.getClass();
            Class<?> superclass = beanClass.getSuperclass();
            Class<?> genericInterfaces = (Class<?>) superclass.getGenericInterfaces()[0];
            ParameterizedType iBaseService = (ParameterizedType) genericInterfaces.getGenericInterfaces()[0];
            Class<?> actualTypeArgument = (Class<?>) iBaseService.getActualTypeArguments()[0];
            if (actualTypeArgument == clazz) {
                basicMpService = (BasicMpService) SpringUtil.getBean(beanClass);
            }
        }

        return basicMpService;
    }
}
